package com.activity.fragment;

import android.content.Intent;
import android.os.Bundle;

import com.activity.App;
import com.activity.SettingActivity;
import com.activity.adapter.AttendanceSetAdapter;
import com.activity.adapter.ClockAdapter;
import com.activity.shop.ClockDetailActivity;
import com.activity.shop.ClockSetActivity;
import com.activity.shop.CreateAttanceGroupActivity;
import com.activity.shop.SheetActivity;
import com.api.param.DeleteClassParam;
import com.api.param.GroupIdParam;
import com.api.param.GroupParam;
import com.api.param.StoreParam;
import com.api.viewModel.StoreVM;
import com.example.apple.ddhr_new.activity.widget.Dialog;
import com.example.apple.ddhr_new.utils.ActivityUtil;
import com.example.apple.ddhr_new.utils.ToastUtil;
import com.google.gson.Gson;

import event.MessageEvent;

import static event.MessageEvent.MESSAGE_EVENT_CREATE_GROUP;

public class AttendanceSetFragment extends BaseListViewFragment<StoreVM> {
    private AttendanceSetAdapter attendanceSetAdapter;

    private StoreParam storeParam=new StoreParam();
    private int selectedIndex;

    @Override
    public void createView() {
        super.createView();
        refreshLayout.setEnableLoadMore(false);
        attendanceSetAdapter = new AttendanceSetAdapter(mContext);
        recycleView.setAdapter(attendanceSetAdapter);
        attendanceSetAdapter.attanceSetListener = new AttendanceSetAdapter.AttanceSetListener() {
            @Override
            public void clockClick() {
                if (App.getInstance().getAccount().getAttendancegroupinfo() != null && App.getInstance().getAccount().getAttendancegroupinfo().size() > 0) {
                    ActivityUtil.startActivity(mContext,ClockSetActivity.class);

                }else {
                    ToastUtil.toast(mContext,"无考勤组信息");
                }

            }

            @Override
            public void sheetClick() {

                Bundle  bundle= new Bundle();
                bundle.putInt("type",0);
                ActivityUtil.startActivity(mContext,SheetActivity.class,bundle);
            }

            @Override
            public void addClick() {
                ActivityUtil.startActivity(mContext,CreateAttanceGroupActivity.class,new Bundle());
            }

            @Override
            public void waiChuClick() {
                Bundle  bundle= new Bundle();
                bundle.putInt("type",1);
                ActivityUtil.startActivity(mContext,SheetActivity.class,bundle);
            }

            @Override
            public void jiabanClick() {
                Bundle  bundle= new Bundle();
                bundle.putInt("type",2);
                ActivityUtil.startActivity(mContext,SheetActivity.class,bundle);
            }

            @Override
            public void deleteClick(int childPosition) {




                Dialog.alert(mContext, "是否删除考勤组?", "", new Dialog.OnDialogOkClickListener() {
                    @Override
                    public void onClick(Object o) {

                    }
                }, new Dialog.OnDialogOkClickListener() {
                    @Override
                    public void onClick(Object o) {
                        selectedIndex =childPosition;
                        GroupIdParam groupIdParam = new GroupIdParam();
                        groupIdParam.groupid = attendanceSetAdapter.getData().get(childPosition).getAttendancegroupid();
                        groupIdParam.groupno = attendanceSetAdapter.getData().get(childPosition).getAttendancegroupno();

                        addSubscription(viewModel.deleteGroup(groupIdParam ));
                    }
                });

            }


            @Override
            public void updateClick(int childPosition) {
                Bundle intent =new Bundle();
                Gson gson =new Gson();
                intent.putString("param",gson.toJson(attendanceSetAdapter.getData().get(childPosition)));
                ActivityUtil.startActivity(mContext,CreateAttanceGroupActivity.class,intent);
            }
        };

        viewModel.attanceGroup.observe(this,res->{


            headerFreshEnd();
            if(res!=null)
            {
                attendanceSetAdapter.getData().clear();
                attendanceSetAdapter.getData().addAll(res);
                attendanceSetAdapter.notifyDataChanged();
            }
        });

        viewModel.deleteGroup.observe(this,res->{

            attendanceSetAdapter.getData().remove(selectedIndex);
            attendanceSetAdapter.notifyDataChanged();

        });



        headerFreshing();
    }

    @Override
    protected void headerFreshing() {
        addSubscription(viewModel.getGroups(storeParam));
    }

    @Override
    public void onMessageEventBus(MessageEvent event) {
        super.onMessageEventBus(event);
        if(event.getMessage()==MESSAGE_EVENT_CREATE_GROUP){
            headerFreshing();
        }
    }
}
